package com.gombe.credit.app.ui.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Typeface
import android.os.CountDownTimer
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StyleSpan
import android.view.LayoutInflater
import android.view.View
import com.gombe.credit.app.R
import com.gombe.credit.app.common.CommonDialog
import com.gombe.credit.app.databinding.DialogGombeCreditUserDataDeleteBinding
import com.gombe.credit.app.event.GombeCreditEventPointType

/**
 * GombeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/27 15:50
 * @Annotation：清空用户数据dialog
 */
class DialogGombeCreditUserDataDelete: CommonDialog {

    private lateinit var binding: DialogGombeCreditUserDataDeleteBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    //等待时长
    private var countdownTimer: MyCountDownTimer? = null
    private var countdown = 10

    constructor(context: Context) : super(context) {}

    constructor(context: Context, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun takeGombeCreditRootView(): View {
        binding = DialogGombeCreditUserDataDeleteBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initGombeCreditView() {

    }

    override fun initGombeCreditListener() {
        setOnClickListener(binding.imageClose)
        setOnClickListener(binding.buttonReturn)
        setOnClickListener(binding.buttonConfirm)
    }

    override fun takeGombeCreditData() {

    }

    override fun onGombeCreditViewClick(v: View?) {
        dismiss()
        when(v?.id){
            R.id.buttonConfirm -> {
                mCallback?.onUserDataClearConfirmClick()
            }
        }
    }

    fun showGombeCreditUserDataDeleteInfo(desc: String, date: String){
        val showDesc = "$desc $date."
        val descSpannable = SpannableString(showDesc)
        val startIndex = showDesc.indexOf(date)
        descSpannable.setSpan(ForegroundColorSpan(getColor(R.color.gombe_credit_ffff2f00)), startIndex, startIndex + date.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        descSpannable.setSpan(StyleSpan(Typeface.BOLD), startIndex, startIndex + date.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        descSpannable.setSpan(RelativeSizeSpan(1.1f),startIndex,startIndex+date.length,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        binding.textDeleteDataDesc.text = descSpannable
    }

    interface IDialogCallback {
        //按钮点击
        fun onUserDataClearConfirmClick()
    }

    class MyCountDownTimer(private val dialog: DialogGombeCreditUserDataDelete, millisInFuture: Int, countDownInterval: Long): CountDownTimer(millisInFuture.toLong(), countDownInterval) {
        override fun onTick(millisUntilFinished: Long) {
            // 这里每隔一段时间（定时器间隔）会被调用
            // 可以在这里更新UI，显示剩余时间
            dialog.countdown = (millisUntilFinished / 1000).toInt()
            dialog.checkClearUserDataConfirmStatus()
        }
        override fun onFinish() {
            // 倒计时完成时被调用
            dialog.checkClearUserDataConfirmStatus()
        }
    }

    /**
     * 启动倒计时
     */
    @SuppressLint("SetTextI18n")
    private fun startCountdownTimer() {
        //随机 60秒 等待时长
        countdown = 10
        checkClearUserDataConfirmStatus()
        if (countdownTimer == null){
            countdownTimer = MyCountDownTimer(this, countdown * 1000, 1000)
        }
        countdownTimer?.start()
    }

    @SuppressLint("SetTextI18n")
    private fun checkClearUserDataConfirmStatus(){
        if (countdown <= 0){
            binding.buttonConfirm.isEnabled = true
            binding.buttonConfirm.text = getString(R.string.gombe_credit_button_confirm)
        } else {
            binding.buttonConfirm.isEnabled = false
            binding.buttonConfirm.text = "${getString(R.string.gombe_credit_button_confirm)}(${countdown}s)"
        }
    }

    override fun onStart() {
        super.onStart()
        startCountdownTimer()
        addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_DIALOG.USER_DATA_CLEAR_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        if (countdownTimer != null) {
            countdownTimer?.cancel()
            countdownTimer = null
        }
        addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_DIALOG.USER_DATA_CLEAR_DISMISS,eventRemark)
    }
}